package top.ajunnihao.core.web.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import top.ajunnihao.core.entity.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 管理员登录拦截器
 * </p>
 */
@Component
public class AdminLoginInterceptor implements HandlerInterceptor {

    private static final String ADMIN = "ADMIN";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute(ADMIN);
        if (admin == null) {
            // 管理员未登录
            request.setAttribute("loginMsg", "暂无权限，请先登录!");
            request.getRequestDispatcher("/admin/login").forward(request, response);
            return false;
        }
        return true;
    }

}
